#!/usr/bin/env bash
# FOSSology fo-installdeps script
# SPDX-FileCopyrightText: © 2008-2014 Hewlett-Packard Development Company, L.P.

# SPDX-License-Identifier: GPL-2.0-only
#
# This script helps you install build and runtime dependencies on a system.
# It is NOT indented to replace package dependencies, it's just a tool to
# make testing the "upstream" build and install process. If you determine
# this script isn't installing something you think it should, consult
# the packaging metadata for the system in question as that is the
# canonical location for such info, then fix it there first and also
# update this file and the INSTALL document.

show_help() {
  cat <<EOF
Usage: fo-installdeps [options]
  -r or --runtime    : install runtime dependencies
  -b or --buildtime  : install buildtime dependencies
  -e or --everything : install all dependencies (default)
  -o or --offline : do not run composer installation
  -y                 : Automatic yes to prompts
  -h or --help       : this help text
EOF
}

source "$(dirname ${BASH_SOURCE[0]})/utils.sh"

#
# Don't show the -y option.  Should only be used for install testing, as using
# it without being careful can destroy your system.
#
YesOpt=''

#Default should be installation with internet connection
OFFLINE=''
EVERYTHING=''
RUNTIME=''
BUILDTIME=''

## Options parsing and setup
# parse options
OPTS=$(getopt -o rbeohy --long runtime,buildtime,everything,offline,help -n 'fo-installdeps' -- "$@")

if [[ $? -ne 0 ]]; then
   OPTS="--help"
fi

eval set -- "$OPTS"

# if no options or just -y then do everything
if [[ $OPTS == ' --' || $OPTS == ' -y --' ]]; then
  EVERYTHING=true
fi

while true; do
   case "$1" in
      -r|--runtime)     RUNTIME=true; shift;;
      -b|--buildtime)   BUILDTIME=true; shift;;
      -e|--everything)  EVERYTHING=true; shift;;
      -o|--offline)     OFFLINE=true; shift;;
      -y)               YesOpt='-y'; shift;;
      -h|--help)        show_help; exit;;
      --)               shift; break;;
      *)                echo "ERROR: option $1 not recognised"; exit 1;;
   esac
done

set -o errexit -o nounset -o pipefail

must_run_as_root
need_lsb_release

if [[ $EVERYTHING ]]; then
   echo "*** Installing both runtime and buildtime dependencies ***"
   RUNTIME=true
   BUILDTIME=true
fi

# figure out what distro we're on
DISTRO=$(lsb_release --id --short)
CODENAME=$(lsb_release --codename --short)

########################################################################

echo "install core dependencies"

if [[ $BUILDTIME ]]; then
   echo "*** Installing $DISTRO buildtime dependencies ***";
   case "$DISTRO" in
      Debian|Ubuntu)
         echo "DB: Installing build essential....."
         apt-get $YesOpt install \
            libmxml-dev curl libxml2-dev libcunit1-dev libicu-dev \
            build-essential libtext-template-perl subversion rpm librpm-dev \
            libmagic-dev libglib2.0 libboost-regex-dev libzstd-dev \
            libboost-program-options-dev libpq-dev composer patch devscripts \
            libdistro-info-perl libcppunit-dev libomp-dev cmake ninja-build
         case "$CODENAME" in
           stretch)
             apt-get $YesOpt install php-mbstring php7.0-cli php7.0-xml php7.0-zip php7.0-gd php7.0-pgsql php7.0-curl php7.0-uuid;;
           buster)
             apt-get $YesOpt install php-mbstring php7.3-cli php7.3-xml php7.3-zip php7.3-gd php7.3-pgsql php7.3-curl php7.3-uuid;;
           bullseye)
             apt-get $YesOpt install php7.4-mbstring php7.4-cli php7.4-xml php7.4-zip php7.4-gd php7.4-pgsql php7.4-curl php7.4-uuid;;
           sid)
             apt-get $YesOpt install php-mbstring php-cli php-xml php-zip php-gd php-pgsql php-curl php-uuid;;
           focal)
             apt-get $YesOpt install php-mbstring php-cli php-xml php-zip php-gd php-pgsql php-curl php-uuid;;
           jammy)
             apt-get $YesOpt install php-mbstring php-cli php-xml php-zip php-gd php-pgsql php-curl php-uuid;;
         esac
         if ! dpkg --get-selections | grep -q postgresql-server-dev; then  ## if postgresql-server-dev is not installed
           case "$CODENAME" in
              stretch)
                apt-get $YesOpt install postgresql-server-dev-9.6;;
              buster)
                apt-get $YesOpt install postgresql-server-dev-11;;
              bullseye)
                apt-get $YesOpt install postgresql-server-dev-13;;
              sid)
                apt-get $YesOpt install postgresql-server-dev-11;;
              focal)
                apt-get $YesOpt install postgresql-server-dev-12;;
              jammy)
                apt-get $YesOpt install postgresql-server-dev-14;;
              *) echo "ERROR: Unknown or Unsupported $DISTRO $CODENAME release, please report to the mailing list"; exit 1;;
           esac
         fi
         ;;
      Fedora)
         yum $YesOpt groupinstall "Development Tools"
         yum $YesOpt install \
            perl-Text-Template subversion \
            postgresql-devel file-devel \
            libxml2 \
            boost-devel php-mbstring libicu-devel libpq-devel patch \
            libomp-devel cmake ninja-build
         ;;
      RedHatEnterprise*|CentOS)
         yum $YesOpt install \
            postgresql-devel \
            gcc make file libxml2 \
            perl-Text-Template subversion \
            boost-devel php-mbstring libicu-devel libpq-devel patch \
            libomp-devel cmake ninja-build
         ;;
      *) echo "ERROR: distro not recognised, please fix and send a patch"; exit 1;;
   esac
fi

########################################################################

if [[ $RUNTIME ]]; then
   echo "*** Installing $DISTRO runtime dependencies ***";
   echo "*** For optional email notifications a Mail Transport Agent Must be ***";
   echo "*** installed. Consult with your system administrator. Or try ***";
   echo "*** apt-get install mail-transport-agent, pick one and install it***";
   case "$DISTRO" in
      Debian|Ubuntu)
        echo "doing runtime"
         apt-get $YesOpt install apache2
         apt-get $YesOpt install php-pear \
            libxml2 libzstd1 \
            binutils \
            cabextract cpio sleuthkit genisoimage \
            poppler-utils upx-ucl \
            unrar-free unzip p7zip-full p7zip wget \
            subversion git \
            dpkg-dev php-uuid
         case "$CODENAME" in
            stretch)
               apt-get $YesOpt install postgresql-9.6 php7.0 php7.0-pgsql libapache2-mod-php7.0 php7.0-pgsql \
                 php7.0-cli php7.0-curl php7.0-xml php7.0-zip php7.0-mbstring php7.0-uuid php-gettext s-nail libboost-program-options1.62.0 \
                 libboost-regex1.62.0 libicu57;;
            buster)
               apt-get $YesOpt install postgresql-11 php7.3 php7.3-pgsql libapache2-mod-php7.3 php7.3-pgsql \
                 php7.3-cli php7.3-curl php7.3-xml php7.3-zip php7.3-mbstring php7.3-uuid php-gettext s-nail libboost-program-options1.67.0 \
                 libboost-regex1.67.0 libicu63;;
            bullseye)
               apt-get $YesOpt install postgresql-13 php7.4 php7.4-pgsql \
               libapache2-mod-php7.4 php7.4-pgsql php7.4-cli php7.4-curl \
               php7.4-xml php7.4-zip php7.4-mbstring php7.4-uuid php-php-gettext s-nail \
               libboost-program-options1.74.0 libboost-regex1.74.0 libicu67;;
            sid)
               apt-get $YesOpt install postgresql php php-pgsql php-cli php-curl php-xml php-zip php-mbstring php-uuid php-gettext s-nail \
                 libboost-program-options1.74.0 libboost-regex1.74.0 libicu67;;
            focal)
               apt-get $YesOpt install postgresql php php-pgsql php-cli php-curl php-xml php-zip php-mbstring php-uuid s-nail \
                 libboost-program-options1.71.0 libboost-regex1.71.0 libicu66;;
            jammy)
               apt-get $YesOpt install postgresql-14 php8.1 php8.1-pgsql \
               libapache2-mod-php8.1 php8.1-pgsql php8.1-cli php8.1-curl \
               php8.1-xml php8.1-zip php8.1-mbstring php8.1-uuid php-php-gettext s-nail \
               libboost-program-options1.74.0 libboost-regex1.74.0 libicu70;;
            *) echo "ERROR: Unknown or Unsupported $DISTRO $CODENAME release, please report to the mailing list"; exit 1;;
         esac
         ;;
      Fedora)
         yum $YesOpt install postgresql-server httpd chkconfig
         yum $YesOpt install \
            postgresql \
            php php-pear php-pgsql php-process php-xml php-mbstring\
            smtpdaemon \
            libxml2 \
            binutils mailx \
            sleuthkit boost libicu

         # enable, possible init, and start postgresql
         /sbin/chkconfig postgresql on
         if [ ! -f /var/lib/pgsql/data/PG_VERSION ]; then
            /usr/bin/postgresql-setup --initdb
         fi
         /usr/bin/systemctl start postgresql

         echo "NOTE: unrar is not available in Fedora release $CODENAME,"
         echo "   please install from upstream sources."
         ;;
      RedHatEnterprise*|CentOS)
         yum $YesOpt install postgresql-server httpd
         yum $YesOpt install \
            postgresql \
            php php-pear php-pgsql php-process php-mbstring\
            smtpdaemon \
            file libxml2 \
            binutils mailx boost libicu
         echo "NOTE: cabextract, sleuthkit, upx, and unrar are not"
         echo "    available in RHEL please install from upstream sources."
         ;;
      *) echo "ERROR: distro not recognised, please fix and send a patch"; exit 1;;
   esac

   if [[ $OFFLINE ]]; then
       echo "WARNING: composer is not installed, and the installer does not install it due to the offline switch"
       echo "Please run the equivalent of 'sudo ./utils/install_composer.sh'"
   else
       "$( dirname "$0" )/install_composer.sh"
   fi
fi

########################################################################
options=''
if [[ $BUILDTIME ]]; then
  options="$options -b"
fi

if [[ $RUNTIME ]]; then
  options="$options -r"
fi

if [[ $YesOpt ]]; then
  options="$options -y"
fi
mods_dir="$(dirname "$0")/../src/" ## get the directory where fo-installdeps resides

find "$mods_dir" -type f -name mod_deps -execdir bash -cv "{} $options" \;

########################################################################
